<?php
//首页显示
namespace newMobile\Controller;
use Think\Controller;
class ListController extends CommonController {
    public function catalog(){
    	$this->display();
	}
	
	public function index(){
		$id=I('get.id');
    	$ticket_type=M('ticket_type');
    	$brand=M('brand');
    	$shop_type=M('shop_type');
    	$ticket_types=$ticket_type->find($id);
    	$this->assign('ticket_types',$ticket_types);
    	//筛选价格
    	$shop_type_info=$shop_type->find($ticket_types['type_id']);
    	$shop_type_info['price'] = str_replace(" ","",$shop_type_info['price']);
    	$price_array=explode(',',$shop_type_info['price']);
    	$this->assign('price_array',$price_array);
    	 //获取品牌
        $bindBrand=$ticket_types['bindBrand'];
       	$brand_array=$brand->where('id in ('.$bindBrand.')')->select();
        $this->assign('brand_array',$brand_array);
    	
    	//筛选属性生成
    	$ticket_config=M('ticket_config');
    	$ticket_atr=$ticket_config->where('ishidden=0 and type_id='.$ticket_types['type_id'])->select();
    	foreach ($ticket_atr as $key => $value) {
    		$ticket_atr[$key]['content']=explode(',',$value['content']);
    	}
    	$this->assign('ticket_atr',$ticket_atr);
    	
    	
    	$brand=session('brand');
    	$price=session('price');
    	$filter_attr=session('filter_attr');
    	$this->assign('brand',$brand);
    	$this->assign('price',$price);
    	$this->assign('filter_attr',$filter_attr);
    	$this->display();
	
	}
	
	function ajax_getattr(){
		if($_POST){
    		$data=I('post.');
    		
    		if(!$data['price']){
    			$data['price']=null;
    		}
    		if($data['brand']==0){
    			$data['brand']=null;
    		}
    		if($data['filter_attr']==''){
    			$data['filter_attr']=null;
    		}
    		
    		session('brand',$data['brand']);
    		session('price',$data['price']);
    		session('filter_attr',$data['filter_attr']);
    		
    	}
	}
	function clear_getattr(){
		session('brand',null);
    	session('price',null);
    	session('filter_attr',null);
	}
	function ajax_getshop(){
		
		$brand=session('brand');
    	$price=session('price');
    	$filter_attr=session('filter_attr');
    	$id=I('get.id');
    	$order=I('get.order',0);
    	$last=I('post.last');
    	$amount=I('post.amount');
        $types=I('get.types');
    	$where_lianhe='ishidden=0 and FIND_IN_SET('.$id.',type_id)';//联合查询条件
	    if($brand!=0&&$brand!=null){
	    	$where_lianhe.=' and brand_id='.$brand;
	    }
	    if($filter_attr){
	    	$filter_attr=explode(',',$filter_attr);
	    	foreach($filter_attr as $k=>$v){   
			    if($v){
			    	$where_lianhe.=' and FIND_IN_SET("'.$v.'",search_tag) ';
			    } 
			}  
	    }
	    
	    if($price){
	    	$price=explode('-',$price);
	    	
	    	$where_lianhe.=' and price>='.$price[0].' and price<='.$price[1];
	    }
	    
	  	
	  	$orders_arr=array(
   			'0' => 'id desc', 
   			'1' => 'sale_num desc', 
   			'2' => 'click_num desc', 
   			'3' => 'price desc', 
   			'4' => 'price asc', 
   		);
   		
	    $shop=M('shop');
		$rs=$shop->where($where_lianhe)->order($orders_arr[$order])->limit($last,$amount)->select();
		
		foreach($rs as $key=>$value){
            $rs_type=shop_type($value['id']);
            if($rs_type['tuan']==1){
            	$tuan='<div class="pic_box"><img src="/static/images/tuan.png" alt="此商品参与团购" style="position: absolute;z-index: 999;width: auto;height: auto;top: 0;left: 0px;"></div>';
			}else{
                $tuan='';
			}
            if($rs_type['coupon']==1){
                $coupon='<em style="background-image: url(\'/static/images/yhq.png\');padding: 1px 7px 1px 14px;color: #fff;background-size: 100% 100%;margin-right: 5px;font-style: inherit;font-size: 12px;">优惠券</em>';
            }else{
                $coupon='';
            }
	   		 $res[]['info'] = '<li><div class="item">'.$tuan.'<div class="pic_box"></div><div class="goods_images"><a href="'.U('shop/index',array('id'=>$value['id'],'type'=>$types)).'"><img src="'.$value['image'].'"></a></div><dl><dt><a href="'.U('shop/index',array('id'=>$value['id'])).'">'.$coupon.$value['title'].'</a></dt><dd><i>￥'.$value['price'].'</i><em>￥'.$value['oprice'].'</em> </dd><span>已售'.$value['sale_num'].'件</span></dl></div><!--列表页面加减号--></li>';
	    }
	    $this->ajaxReturn($res);
	}
	
	
	function group_buy(){
        $time=time();
        $where['ishidden']=0;
        $where['tuanselect']=array('eq',1);
        $where['tuanz_endtime']=array('gt',$time);
        $shop=M('shop');
        $shop_list	= $shop->where($where)->select();
        $types=array();
        foreach ($shop_list as $k=>$v){
            $type_ids=explode(',',$v['type_id']);
            foreach ($type_ids as $key=>$value){
            	if($value&&!in_array($value,$types)){
                    $types[]=$value;
				}
			}

		}
        $types_string=implode(',',$types);
        $map['id']=array('in',$types_string);

        $rs=M('ticket_type')->where($map)->select();

        $this->assign('rs',$rs);
		$this->display();
	}
	function ajax_group(){
		$last=I('post.last');
    	$amount=I('post.amount');
        $type=I('get.type');
    	$orders=I('get.order');
        if($orders==0){
            $order='id desc';
        }
        if($orders==1){
            $order='sale_num desc';
        }
        if($orders==3){
            $order='price desc';
        }
        if($orders==4){
            $order='price asc';
        }


		$time=time();
		$where['ishidden']=0;
	    $where['tuanselect']=array('eq',1);
		$where['tuanz_endtime']=array('gt',$time);
        if($type!=0){
            $where['_string']=" FIND_IN_SET('".$type."',type_id)";
        }
    	$shop=M('shop');
    	$shop_list	= $shop->where($where)->limit($last,$amount)->order($order)->select();

    		foreach($shop_list as $key=>$value){
	   		 $res[]['info'] = '<li>
					<span class="brand-name">'.$value['title'].'</span>
					<div class="p-img">
						<a href="'.U('shop/index',array('id'=>$value['id'])).'">
							<img src="'.$value['image'].'">
						</a>
						<div class="brand-time">
							<i></i>
							<span class="settime" endtime="'.date('Y-m-d H:i:s',$value['tuanz_endtime']).'"></span>
						</div>
					</div>
					<div class="brand-info">
						<div class="brand-info-left">
							<p class="b-price">
								'.$value['tuan_price'].'
								<em>元</em>
							</p>
							<p class="buyer">'.$value['sale_num'].'人已抢</p>
						</div>
						<div class="brand-info-right">
								<div class="core"><a href="'.U('shop/index',array('id'=>$value['id'])).'"><span>'.$value['tuan_num'].'人团</span><span class="tmp"></span><span>去开团</span></a></div>
							
						</div>
					</div>
				</li>';
	    }
	    $this->ajaxReturn($res);
    	 
	}
	

	
	function red_envelope(){
		 $this->display();
	}
	
	function ajax_red_envelope(){
		$last=I('post.last');
    	$amount=I('post.amount');
    	$order=I('get.order',0);
    	$orders_arr=array(
   			'0' => 'id desc', 
   			'1' => 'sale_num desc', 
   			'2' => 'click_num desc', 
   			'3' => 'price desc', 
   			'4' => 'price asc', 
   		);
		$shop=M('shop');
		$where_lianhe='ishidden=0 and redenvelope>0';//联合查询条件
		$rs=$shop->where($where_lianhe)->order($orders_arr[$order])->limit($last,$amount)->select();
		$get_web_invoice=$this->$get_web_invoice;
		foreach($rs as $key=>$value){
			if($get_web_invoice['web_daiyan']>=$value['redenvelope']){
				$value['redenvelope']=round($value['redenvelope']/$value['price'] * 100 , 2) . "％";
			}else{
				$value['redenvelope']='￥'.$value['redenvelope'];
			}
			if($value['tuanselect']==0){
                if($value['envelope_type']==0){
                    $type='代言红包';
                    $back_color="#e9394e";
                }else{
                    $type='代言礼包';
                    $back_color="#e9394e";
                }

			}else{
                if($value['envelope_type']==0){
                    $type='代言红包';
                    $back_color="#fbca60";
                }else{
                    $type='代言礼包';
                    $back_color="#fbca60";
                }
			}
            if($value['envelope_type']==0){
                $res[]['info'] = '<li><div class="item"><div class="pic_box"></div><div class="goods_images"><a href="'.U('shop/index',array('id'=>$value['id'])).'"><img src="'.$value['image'].'"></a></div><dl><dt><a href="'.U('shop/index',array('id'=>$value['id'])).'">'.$value['title'].'</a></dt><dd><i>￥'.$value['price'].'</i><em>￥'.$value['oprice'].'</em> </dd><span style="background: '.$back_color.';color: #fff;padding: 0 7px;border-radius: 11px;font-size: 12px;"><a href="'.U('shop/index',array('id'=>$value['id'])).'" style="color:#fff;">'.$type.'：'.$value['redenvelope'].'</a></span></dl></div><!--列表页面加减号--></li>';
            }else{
                $res[]['info'] = '<li><div class="item"><div class="pic_box"></div><div class="goods_images"><a href="'.U('shop/index',array('id'=>$value['id'])).'"><img src="'.$value['image'].'"></a></div><dl><dt><a href="'.U('shop/index',array('id'=>$value['id'])).'">'.$value['title'].'</a></dt><dd><i>￥'.$value['price'].'</i><em>￥'.$value['oprice'].'</em> </dd><span style="background: '.$back_color.';color: #fff;padding: 0 7px;border-radius: 11px;font-size: 12px;"><a href="'.U('shop/index',array('id'=>$value['id'])).'" style="color:#fff;">'.$type.'</a></span></dl></div><!--列表页面加减号--></li>';
            }

	    }
	    $this->ajaxReturn($res);
	}
	
	function coupons(){
        $store_id=I('get.id');
        if($store_id){
            $store_template=M('store_template');
            $store_template_info=$store_template->where('store_id='.$store_id)->find();
            $store=M('store');
            $store_info=$store->find($store_id);
            $this->assign('store_info',$store_info);
            $this->assign('store_template_info',$store_template_info);

            //判断收藏
            $users_storecollect=M('users_storecollect');
            if(session('user_id')){

                $newstoreinfo['shore_id']=$store_id;
                $newstoreinfo['user_id']=session('user_id');
                $rsstoreinfo=$users_storecollect->where($newstoreinfo)->select();
                if($rsstoreinfo){
                    $rsstoreinfo_status=1;
                }else{
                    $rsstoreinfo_status=0;
                }
            }else{
                $rsstoreinfo_status=0;
            }
            $this->assign('rsstoreinfo_status',$rsstoreinfo_status);
		}
        if($_GET['type_id']){
            $type_id=I('get.type_id');

            $coupon_info=M('coupon_type')->FIND($type_id);
            $title=$coupon_info['coupon_name'];
        }else{

            $title="优惠券";
        }
        $this->assign('title',$title);
		 $this->display();
	}
	
	function ajax_coupons(){
		$last=I('post.last');
    	$amount=I('post.amount');
    	$order=I('get.order',0);
    	$store_id=I('get.store_id',0);
    	$orders_arr=array(
   			'0' => 'id desc', 
   			'1' => 'sale_num desc', 
   			'2' => 'click_num desc', 
   			'3' => 'price desc', 
   			'4' => 'price asc', 
   		);
        $coupon_goods=M('coupon_goods');
    	if($_GET['type_id']){
			$type_id=I('get.type_id');
            $goods_ids=$coupon_goods->where('coupon_type_id='.$type_id)->field('goods_id')->group('goods_id')->select();

		}else{
            $goods_ids=$coupon_goods->field('goods_id')->group('goods_id')->select();

		}
    	$new_goods_ids=array();
   		foreach($goods_ids as $key =>$value){
   			$new_goods_ids[]=$value['goods_id'];
   		}
   		$new_goods_ids=implode(',',$new_goods_ids);
   		$shop=M('shop');
		$where_lianhe="ishidden=0  and id IN ($new_goods_ids)";//联合查询条件

		if($store_id>0){
			$where_lianhe=$where_lianhe.' and store_id='.$store_id;
		}

		$rs=$shop->where($where_lianhe)->order($orders_arr[$order])->limit($last,$amount)->select();

		foreach($rs as $key=>$value){
			//获取优惠券
			$coupon_goods=M('coupon_goods');
			$coupon_type_id=$coupon_goods->field('coupon_type_id')->where("goods_id=".$value['id'])->group('coupon_type_id')->select();
			$coupon_type_ids;
			foreach($coupon_type_id as $k=>$v){
				if($k==0){
					$coupon_type_ids=$v['coupon_type_id'];
				}else{
					$coupon_type_ids=','.$v['coupon_type_id'];
				}
			}
			$coupon_type=M('coupon_type');
			$map['coupon_type_id']  = array('in',$coupon_type_ids);
			$map['range_type']  = 1;
			$map['_logic'] = 'OR';
			$coupons=$coupon_type->where($map)->select();
			$coupons_i=0;
			$new_coupons;
			$html="<span style='float: left;'>";
			
			if($coupons){
				foreach($coupons as $k1=>$v1){
					$start_time=strtotime($v1['start_time']);
					$end_time=strtotime($v1['end_time']);
					$nowtime=time();
					$flag=false;
					if($start_time<=$nowtime && $end_time>=$nowtime){
                        $flag=true;
						$new_coupons[$coupons_i]=$v1;
						$coupons_i++;
						if($v1['at_least']>0){
							$html.="<em style='display: block;float: left;margin-right: 5px; background: red;color: #fff;padding: 0 5px; height: 20px;line-height: 20px;margin-top: 10px;border-radius: 3px;'>满".$v1['at_least'].'减'.$v1['money']."</em>";
						}else{
							$html.="<em style='display: block;float: left;margin-right: 5px; background: red;color: #fff;padding: 0 5px; height: 20px;line-height: 20px;margin-top: 10px;border-radius: 3px;'>立减".$v1['money']."</em>";
						}

					}
				}
			     $html.="</span>";
			    //end
				 $redenvelope=$value['redenvelope']*100;
				 if($flag){
                     $res[]['info'] = '<li><div class="item"><div class="pic_box"></div><div class="goods_images"><a href="'.U('shop/index',array('id'=>$value['id'],'lin'=>1)).'"><img src="'.$value['image'].'"></a></div><dl><dt><a href="'.U('shop/index',array('id'=>$value['id'],'lin'=>1)).'">'.$value['title'].'</a></dt><dd><i>￥'.$value['price'].'</i><em>￥'.$value['oprice'].'</em> </dd>'.$html.'<span style="border: 1px solid #e9394e;padding: 0 7px;border-radius: 7px;font-size: 12px;"><a style="color: #f00;" href="'.U('shop/index',array('id'=>$value['id'],'lin'=>1)).'">立即领取</a></span></dl></div><!--列表页面加减号--></li>';

                 }
	   		 }
	    }
	    $this->ajaxReturn($res);
	}
}
?>
